<?php

/**
 * This is the model class for table "pejabat".
 *
 * The followings are the available columns in table 'pejabat':
 * @property string $nip
 * @property string $kode_jab
 * @property string $nama_pejabat
 * @property string $golongan
 * @property string $ruang
 *
 * The followings are the available model relations:
 * @property Jabatan $kodeJab
 * @property User[] $users
 */
class Pejabat extends CActiveRecord
{
    public $nama_jab;
    /**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'pejabat';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('nip', 'required'),
			array('nip', 'length', 'max'=>20),
			array('kode_jab', 'safe'),
			array('nama_pejabat', 'length', 'max'=>50),
			array('golongan', 'length', 'max'=>5),
			array('ruang', 'length', 'max'=>1),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('nip, nama_pejabat, golongan, ruang', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'kodeJab' => array(self::BELONGS_TO, 'Jabatan', 'kode_jab'),
			'users' => array(self::HAS_MANY, 'User', 'nip'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'nip' => 'Nip',
			'kode_jab' => 'Kode Jab',
			'nama_pejabat' => 'Nama Pejabat',
			'golongan' => 'Golongan',
			'ruang' => 'Ruang',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;
                $criteria->alias="p";
                $criteria->select = 'p.*, j.nama_jab';
                $criteria->join = 'inner join jabatan j ON j.kode_jab = p.kode_jab';

		$criteria->compare('nip',$this->nip,true);
		$criteria->compare('kode_jab',$this->kode_jab,true);
		$criteria->compare('nama_pejabat',$this->nama_pejabat,true);
		$criteria->compare('golongan',$this->golongan,true);
		$criteria->compare('ruang',$this->ruang,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Pejabat the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
